/// Text Overlay Application: Cleaning Class
/// this class contains necessary functions to remove unused fonts and combos
/// from the sistem. These functions are meant to be called periodically to 
/// avoid depleating the system's memory


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.Runtime.InteropServices;
using CommLayer;
using Tamir.IPLib;
using Tamir.IPLib.Packets;
using Magnolia;

namespace TextOverlay
{
    public partial class TextOverlay
    {

        /// <summary>
        /// Searches the Font Library for unused fonts and delete them
        /// </summary>
        private void CleanFontLib()
        {
            for (int x = 0; x < DXFontLib.Count; x++)
            {
                if (FindFont(x) != true)
                    DXFontLib.RemoveAt(x);
            }
        }

        /// <summary>
        /// Searches for a combo in any timeline using the requested font
        /// </summary>
        /// <param name="FontID">Font ID number in the Font Library</param>
        /// <returns>True if the font is being used at least for one combo</returns>
        private bool FindFont(int FontID)
        {
            foreach (CTimeline TL in Timeline)
            {
                foreach (Combo CB in TL.DXComboLib)
                {
                    foreach (Element EL in CB.Fragment)
                    {
                        if (EL.FontID == FontID)
                            return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Searches a Combo Library for unused Combos and delete them
        /// </summary>
        private void CleanComboLib()
        {
            for(int t = 0; t < Timeline.Length; t++)
            {
                for(int c = 0; c < Timeline[t].DXComboLib.Count; c++)
                {
                    if (FindCombo(c, t) != true)
                        Timeline[t].DXComboLib.RemoveAt(c);
                }
            }
        }

        /// <summary>
        /// Searches the timeline for any block using the requested Combo
        /// </summary>
        /// <param name="ComboID">The ID of the Combo in the Combo Library</param>
        /// <param name="TimelineID">The ID of the Timeline</param>
        /// <returns>True if the Combo is used at least for one Block</returns>
        private bool FindCombo(int ComboID, int TimelineID)
        {
            foreach (Block BL in Timeline[TimelineID].Schedule[0].SchCombo )
            {
                if (BL.ComboIndex == ComboID)
                    return true;
            }
            return false;
        }

    }
}